package graph;

import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

public class GraphFactory {

	/**
	 * 
	 * @return A directed Sparse multi graph
	 */

	public static Graph<GraphNode, GraphEdge> createDirectedSparseMultigraph(
			Map<Integer, GraphNode> vertices, Map<Integer, GraphEdge> edges)
			throws IOException {

		DirectedSparseMultigraph<GraphNode, GraphEdge> g = new DirectedSparseMultigraph<GraphNode, GraphEdge>();
		Set<Integer> keys = vertices.keySet();
		for (Integer key : keys) {
			NetNode node = (NetNode) vertices.get(key);
			g.addVertex(node);
		}

		// add edges
		Set<Integer> edgekeys = edges.keySet();
		for (int ei : edgekeys) {
			GraphEdge e = edges.get(ei);

			g.addEdge(e, vertices.get(e.getSrcId()),
					vertices.get(e.getDestId()));
		}

		return g;
	}

	/**
	 * 
	 * @return A directed sparse simple graph
	 */

	public static Graph<GraphNode, GraphEdge> createDirectedSparseGraph(
			Map<Integer, GraphNode> vertices, Map<Integer, GraphEdge> edges)
			throws IOException {

		DirectedSparseGraph<GraphNode, GraphEdge> g = new DirectedSparseGraph<GraphNode, GraphEdge>();
		Set<Integer> keys = vertices.keySet();
		for (Integer key : keys) {
			NetNode node = (NetNode) vertices.get(key);
			g.addVertex(node);
		}

		// add edges
		Set<Integer> edgekeys = edges.keySet();
		for (int ei : edgekeys) {
			GraphEdge e = edges.get(ei);

			g.addEdge(e, vertices.get(e.getSrcId()),
					vertices.get(e.getDestId()));
		}

		return g;
	}
}
